<?php

/**
 * Description of Moneyclass
 *
 * @author perrine
 */
class Money {

    private $value;
    private $currency;   

    function __construct($value,$currency) {
        $this->value=$value;
        $this->currency=strtoupper($currency);

    }

    function getValue() { return $this->value; }
    
    function getCurrency() {return $this->currency; }
    
    function toString() {return "(".$this->currency.") ".$this->value;}
    
    function sameCurrency($mOther) {
        return strcmp($this->getCurrency(),$mOther->getCurrency())==0;
    }
    
    function equals(Money $x) {
        if (!$x->getCurrency()==$this->getCurrency()) {
            return false;
        }
        return $x->getValue()==$this->getValue();
    }


    function convertTo($money, $currency)
    {

        //tableau à deux dimensions représentant le taux de changes entre les diverses monnaies
        $taux = array();
        $taux[0][0]=1;
        $taux[0][1]=1.34626;
        $taux[0][2]=1.24906;
        $taux[1][0]=0.7428;
        $taux[1][1]=1;
        $taux[1][2]=0.9278;
        $taux[2][0]=0.8006;
        $taux[2][1]=1.07782;
        $taux[2][2]=1;


        $val1;
        $val2;
        switch($money->getCurrency()){
            case "EUR" : 
            $val1 = 0; 
            break;
            case "USD" : 
            $val1 = 1; 
            break;
            case "CHF" : 
            $val1 = 2; 
            break;
        }
        switch($currency){
            case "EUR" : 
            $val2 = 0; 
            break;
            case "USD" : 
            $val2 = 1; 
            break;
            case "CHF" : 
            $val2 = 2; 
            break;
        }
        $newValue = $money->getValue()*$taux[$val1][$val2];
        return new Money($newValue,$currency);
    }
}
?>
